﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class ConditonRemarkController : Controller
    {
        //
        // GET: /ConditonRemark/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
            IEnumerable<ConditonRemarkModels> _ConditonRemarks = _ConditonRemarkRepo.GetConditionRemark();
            return View(_ConditonRemarks);
        }

        //
        // GET: /ConditonRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
            ConditonRemarkModels ConditonRemark = new ConditonRemarkModels();
            ConditonRemark = _ConditonRemarkRepo.GetConditionRemarkById(id);
            return View(ConditonRemark);
        }
        //
        // GET: /ConditonRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ConditonRemarkModels _ConditonRemark = new ConditonRemarkModels();

            TitleMwwDispenserRepo _TitleMwwDispenserRepo = new TitleMwwDispenserRepo();
            IEnumerable<TitleMwwDispenserModels> _TitleMwwDispensers = _TitleMwwDispenserRepo.GetTitleMwwDispenser();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleMwwDispensers
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleMwwDispenserId == selectedId),
                                                         Text = s.TitleMwwDispenserName,
                                                         Value = s.TitleMwwDispenserId.ToString()
                                                     };

            _ConditonRemark.TitleSelect = selectlist;
            return View(_ConditonRemark);
        } 

        //
        // POST: /ConditonRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
                ConditonRemarkModels _ConditonRemark = new ConditonRemarkModels();
                _ConditonRemark.TitleMwwDispenserId = SF.GetInt32(collection["TitleMwwDispenserId"]);
                _ConditonRemark.ConditionRemarkTh = collection["ConditionRemarkTh"];
                _ConditonRemark.ConditionRemarkEng = collection["ConditionRemarkEng"];
                _ConditonRemark.CreateBy = (string)Session["User"];
                bool result = _ConditonRemarkRepo.Create(_ConditonRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ConditonRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
            ConditonRemarkModels _ConditonRemark = new ConditonRemarkModels();
            _ConditonRemark = _ConditonRemarkRepo.GetConditionRemarkById(id);

            TitleMwwDispenserRepo _TitleMwwDispenserRepo = new TitleMwwDispenserRepo();
            IEnumerable<TitleMwwDispenserModels> _TitleMwwDispensers = _TitleMwwDispenserRepo.GetTitleMwwDispenser();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleMwwDispensers
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleMwwDispenserId == selectedId),
                                                         Text = s.TitleMwwDispenserName,
                                                         Value = s.TitleMwwDispenserId.ToString()
                                                     };

            _ConditonRemark.TitleSelect = selectlist;
            return View(_ConditonRemark);
        }

        //
        // POST: /ConditonRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
                ConditonRemarkModels _ConditonRemark = new ConditonRemarkModels();
                _ConditonRemark.Id = id;
                _ConditonRemark.TitleMwwDispenserId = SF.GetInt32(collection["TitleMwwDispenserId"]);
                // _FeederRemark.FeederRemarkTh = collection["FeederRemarkTh"];
                _ConditonRemark.ConditionRemarkTh = collection["ConditionRemarkTh"];
                _ConditonRemark.ConditionRemarkEng = collection["ConditionRemarkEng"];
                _ConditonRemark.UpdateBy = (string)Session["User"];
                bool result = _ConditonRemarkRepo.Edit(_ConditonRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ConditonRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
            ConditonRemarkModels _ConditonRemark = new ConditonRemarkModels();
            
            _ConditonRemark = _ConditonRemarkRepo.GetConditionRemarkById(id);
            return View(_ConditonRemark);
        }

        //
        // POST: /ConditonRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                ConditonRemarkRepo _ConditonRemarkRepo = new ConditonRemarkRepo();
                ConditonRemarkModels _ConditonRemark = new ConditonRemarkModels();
                _ConditonRemark.Id = id;
                _ConditonRemark.UpdateBy= (string)Session["User"];
                bool result = _ConditonRemarkRepo.Delete(_ConditonRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
